import {createRouter, createWebHashHistory} from 'vue-router'
import store from "../store";

const routes = [
    {
        path: '/',
        name: 'Home',
        meta: {
            requeireAuth: true,
        },
        component: () => import('../views/Home.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/registry',
        name: 'Registry',
        component: () => import('../views/Registry.vue')
    },
    {
        path: '/progress',
        name: 'Progress',
        meta: {
            requeireAuth: true,
        },
        component: () => import('../views/Progress.vue')
    },
    {
        path: '/profiles',
        name: 'Profiles',
        meta: {
            requeireAuth: true,
        },
        component: () => import('../views/Profiles.vue')
    },
    {
        path: '/wordtest',
        name: 'WordTest',
        component: () => import('../views/WordTest.vue')
    },
    {
        path: '/404',
        name: 'NotFound',
        component: () => import('../views/404.vue')
    },
    {
        path: '/test',
        name: 'Test',
        component: () => import('../views/Test.vue')
    },
    {
        path: '/:pathMatch(.*)*',
        name: '404',
        redirect: '/404',
        component: () => import('../views/404.vue')
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach(async (to) => {
    const isLogin = store.getters["isLogin"];

    // console.log(to.path + "------"+ isLogin)
    // console.log(to.meta.requeireAuth);

    if (to.path === "/login") {
        if (isLogin) {
            return { name: "Home" };
        }
        return true;
    }
    if (to.meta.requeireAuth) {
        // console.log("222222222:" +  to.path)
        if (!isLogin) {
            return { name: "Login" };
        }
    }
    return true;
});

export default router